import { defineStore } from "pinia";
import { ref, onMounted } from "vue";
import { reqIndexInfo, reqSlideShow } from "@/api/index";
import { ElMessage } from "element-plus";
const useIdenxState = defineStore("useIndexStore", () => {
  const indexInfoList = ref<any>([]); //推荐页面初始数据
  const pageSize = ref<number>(10); //每页条数
  const page = ref<number>(1); //当前页
  const slideShowList = ref<any>([]); //轮播图初始数据
  const initData = async () => {
    //请求初始化推荐页面数据
    try {
      const result = await reqIndexInfo(pageSize.value, page.value);
      indexInfoList.value = result.content;
    } catch (error: any) {
      ElMessage.error(error.value);
    }
  };
  const getSlideShowList = async () => {
    //请求轮播图数据
    try {
      const result = await reqSlideShow();
      slideShowList.value = result.content;
    } catch (error: any) {
      ElMessage.error(error.value);
    }
  };
  //添加 indexInfoList 数据
  const isAppAdd = ref<boolean>(true);
  const addIndexInfoList = async () => {
    page.value++;
    try {
      const result = await reqIndexInfo(pageSize.value, page.value);
      indexInfoList.value.push(...result.content);
      ElMessage.success("加载更多！");
      isAppAdd.value = false;
    } catch (error: any) {
      ElMessage.error(error.value);
    }
  };

  onMounted(() => {
    initData();
    getSlideShowList();
  });
  return { indexInfoList, isAppAdd, slideShowList, addIndexInfoList };
});
export default useIdenxState;
